package Demo06_syn;

// 不安全的买票
public class UnsafeBuyticket {
    public static void main(String[] args) {
        BuyTickets station = new BuyTickets();

        new Thread(station,"苦逼的我").start();
        new Thread(station,"牛逼的你们").start();
        new Thread(station,"可恶的黄牛党").start();
    }
}

class BuyTickets implements Runnable{

    // 票
    private int ticketNums = 10;
    boolean flag = true; // 循环标志，线程停止方式

    @Override
    public void run() {
        // 买票
        while (flag){
            try{
                buy();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    // 买票的方法
    private void buy() throws InterruptedException{
        // 判断是否有票
        if (ticketNums <= 0){
            flag = false;
            return;
        }

        Thread.sleep(1000);

        // 买票
        System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);
    }
}
